package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AwMyPlan;
import com.ruoyi.system.domain.AwPlanDetail;
import com.ruoyi.system.enums.DetailStatusEnum;
import com.ruoyi.system.mapper.AwPlanDetailMapper;
import com.ruoyi.system.service.IAwMyPlanService;
import com.ruoyi.system.service.IAwPlanDetailService;
import com.ruoyi.system.timing.ScheduleTimingPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 详细计划Service业务层处理
 *
 * @author anxinr
 * @date 2023-08-15
 */
@Service
public class AwPlanDetailServiceImpl extends ServiceImpl<AwPlanDetailMapper ,AwPlanDetail> implements IAwPlanDetailService
{
    @Autowired
    private AwPlanDetailMapper awPlanDetailMapper;

    @Autowired
    private IAwMyPlanService planService ;

    /**
     * 查询详细计划
     *
     * @param id 详细计划主键
     * @return 详细计划
     */
    @Override
    public AwPlanDetail selectAwPlanDetailById(Long id)
    {
        return awPlanDetailMapper.selectById(id);
    }

    /**
     * 查询详细计划列表
     *
     * @param awPlanDetail 详细计划
     * @return 详细计划
     */
    @Override
    public List<AwPlanDetail> selectAwPlanDetailList(AwPlanDetail awPlanDetail)
    {
        LambdaQueryWrapper<AwPlanDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!Objects.isNull(awPlanDetail.getPlanId()) ,AwPlanDetail::getPlanId ,awPlanDetail.getPlanId()) ;
        wrapper.eq(!Objects.isNull(awPlanDetail.getStatus()) ,AwPlanDetail::getStatus ,awPlanDetail.getStatus()) ;
        wrapper.like(StringUtils.isNotEmpty(awPlanDetail.getScheduleDescribe()) ,AwPlanDetail::getScheduleDescribe ,awPlanDetail.getScheduleDescribe()) ;
        wrapper.eq(StringUtils.isNotEmpty(awPlanDetail.getStartTime()) ,AwPlanDetail::getStartTime ,awPlanDetail.getStartTime()) ;
        wrapper.eq(StringUtils.isNotEmpty(awPlanDetail.getEndTime()) ,AwPlanDetail::getEndTime ,awPlanDetail.getEndTime()) ;
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            LambdaQueryWrapper<AwMyPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AwMyPlan::getUserId ,SecurityUtils.getUserId()) ;
            List<AwMyPlan> list = planService.list(queryWrapper);
            if (CollectionUtils.isEmpty(list)) {
                wrapper.isNull(AwPlanDetail::getPlanId) ;
            }else {
                wrapper.in(AwPlanDetail::getPlanId ,list.stream().collect(Collectors.toMap(AwMyPlan::getId ,AwMyPlan::getId)).values()) ;
            }
        }
        return this.list(wrapper);
    }

    /**
     * 新增详细计划
     *
     * @param awPlanDetail 详细计划
     * @return 结果
     */
    @Override
    public int insertAwPlanDetail(AwPlanDetail awPlanDetail)
    {
        awPlanDetail.setCreateTime(DateUtils.getNowDate());
        return awPlanDetailMapper.insert(awPlanDetail);
    }

    /**
     * 修改详细计划
     *
     * @param awPlanDetail 详细计划
     * @return 结果
     */
    @Override
    public int updateAwPlanDetail(AwPlanDetail awPlanDetail)
    {
        awPlanDetail.setUpdateTime(DateUtils.getNowDate());
        return awPlanDetailMapper.updateById(awPlanDetail);
    }

    /**
     * 批量删除详细计划
     *
     * @param ids 需要删除的详细计划主键
     * @return 结果
     */
    @Override
    public int deleteAwPlanDetailByIds(Long[] ids)
    {
        if (!CollectionUtils.isEmpty(Arrays.asList(ids))){
            for (int i = 0; i < ids.length; i++) {
                this.removeById(ids[i]) ;
            }
            return ids.length;
        }
        return 0;
    }

    /**
     * 删除详细计划信息
     *
     * @param id 详细计划主键
     * @return 结果
     */
    @Override
    public int deleteAwPlanDetailById(Long id)
    {
        return awPlanDetailMapper.deleteAwPlanDetailById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String completePlanDetail(Long id) {
        AwPlanDetail detail = this.getById(id);
        detail.setStatus(DetailStatusEnum.DETAIL_STATUS_SUCCESS.getCode());
        this.updateById(detail) ;
        boolean result = ScheduleTimingPool.completeTimingTask(id);
        if (!result) {
            throw new ServiceException("修改任务池信息失败！") ;
        }
        return "操作成功";
    }
}


